﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using Table=FCNS.Data.Table;
using System.Diagnostics;
using DoubleH.Utility;
using DoubleH;

namespace UniqueS
{
    /// <summary>
    /// MainWindow.xaml 的交互逻辑
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
        }

        Table.UniqueS orderS = null;

        public void Init(Table.UniqueS.EnumFlag flag, Table.UniqueS tag)
        {
            this.Title = flag.ToString();
            orderS = tag ?? new Table.UniqueS(flag);

            comboBoxName.Text = orderS.Name;
            textBoxNote.Text = orderS.Note;
            doubleUpDownDoubleValue.Value = orderS.DoubleValue;
            textBoxStringValue.Text = orderS.StringValue;
            checkBox1.IsChecked = orderS.Enable == Table.UniqueS.EnumEnable.停用 ? true : false;


            canvasDoubleValue.Visibility = Visibility.Collapsed;
            canvasStringValue.Visibility = Visibility.Collapsed;
            this.Height -= canvasDoubleValue.Height;
            switch (orderS.Flag)
            {
                case Table.UniqueS.EnumFlag.服务级别定义:
                    canvasDoubleValue.Visibility = Visibility.Visible;
                    labelDouble.Content = "响应";

                    canvasDoubleValue.Visibility = Visibility.Visible;
                    this.Height += canvasDoubleValue.Height;
                    break;

                case Table.UniqueS.EnumFlag.区域:
                    canvasDoubleValue.Visibility = Visibility.Visible;
                    labelTime.Visibility = Visibility.Hidden;

                    canvasDoubleValue.Visibility = Visibility.Visible;
                    this.Height += canvasDoubleValue.Height;
                    break;

                case Table.UniqueS.EnumFlag.税率:
                    canvasDoubleValue.Visibility = Visibility.Visible;
                    labelTime.Visibility = Visibility.Hidden;
                    labelDouble.Content = "税率:";
                    doubleUpDownDoubleValue.Minimum = 0.01;
                    doubleUpDownDoubleValue.Maximum = 0.5;
                    doubleUpDownDoubleValue.Increment = 0.01;

                    canvasDoubleValue.Visibility = Visibility.Visible;
                    this.Height += canvasDoubleValue.Height;
                    break;

                case Table.UniqueS.EnumFlag.物流公司:
                    foreach (string str in Table.WuLiuS.WuLiuCompany.Keys)
                        comboBoxName.Items.Add(str);

                    comboBoxName.SelectedItem = orderS.Name;
                    comboBoxName.IsEditable = false;
                    canvasStringValue.Visibility = Visibility.Visible;
                    this.Height += canvasStringValue.Height;
                    break;
            }

            comboBoxName.Focus();
            InitEvent();
        }

        private void InitEvent()
        {
            buttonOk.Click += (ss, ee) => { Save(); };
        }

        private bool ControlsValidating()
        {
            bool b = false;
            if (string.IsNullOrWhiteSpace(comboBoxName.Text))
            {
                MessageWindow.Show("名称不能为空");
                comboBoxName.Focus();
            }
            else
                b = true;

            return b;
        }

        private void Save()
        {
            if (!ControlsValidating())
                return;

            orderS.Name = comboBoxName.Text;
            orderS.Note = textBoxNote.Text;
            orderS.DoubleValue = doubleUpDownDoubleValue.Value.Value;
            orderS.StringValue = textBoxStringValue.Text;
            orderS.Enable = checkBox1.IsChecked == true ? Table.UniqueS.EnumEnable.停用 : Table.UniqueS.EnumEnable.启用;

            Table.DataTableS.EnumDatabaseStatus result = Table.DataTableS.EnumDatabaseStatus.操作成功;
            if (orderS.Id == -1)
                result = orderS.Insert();
            else
                result = orderS.Update();

            if (result == Table.DataTableS.EnumDatabaseStatus.操作成功)
                this.Close();
            else
            {
                MessageWindow.Show(result.ToString());
                comboBoxName.Focus();
            }
        }
    }
}
